layui.use(['form', 'layer','jquery'],
    function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;

        //自定义验证规则
        form.verify({
            nikename: function(value) {
                if (value.length < 2) {
                    return '昵称至少得2个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function(value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            },
            phone: [/(.+){11}$/, '手机号必须是11位']
        });

        //监听提交
        form.on('submit(add)',
            function(data) {
                //发异步，提交数据
                $.ajax({
                    url:'/operator/saveOperator',
                    type:'POST',
                    async:true,
                    data: JSON.stringify(data.field),
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    contentType: "application/json",
                    success:function(data){
                        if (data.code == "0"){
                            layer.msg("增加成功", {
                                    icon: 6,
                                    time:1000
                                },
                                function() {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        }else {
                            layer.alert(data.msg, {
                                icon: 5});
                        }
                    }
                });

                return false;
            });

    });